package com.glela.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.glela.common.LocalCache;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.service.BaseRestrictionsService;
import com.mysql.jdbc.util.BaseBugReport;


@Controller
@RequestMapping(value = "/localcache")
public class FilterController {
	private static Logger logger = LoggerFactory.getLogger(FilterController.class);
	
	
	
	@Autowired
	private BaseRestrictionsService baseRestrictionsService;
	
	//显示当前本地缓存内容，根据key  ，通用
	@RequestMapping(value="/show")
	public @ResponseBody ResponseEntity<?> showLocalCache(String key){
		Map<String,Object> map = LocalCache.localCache.get(key);
		return new ResponseEntity<>( map , HttpStatus.OK);
	}
	
	//清除本地缓存，根据key ，通用
	@RequestMapping(value="/clean")
	public @ResponseBody ResponseEntity<?> cleanLocalCache(String key){
		Map<String,Object> map = new HashMap<String,Object>() ;
		LocalCache.localCache.setKey(key, map);
		return new ResponseEntity<>( map , HttpStatus.OK);
	}
	
	
	//更新拦截URL
	@RequestMapping(value="/update_local_filter_url")
	public @ResponseBody ResponseEntity<?> updateLocalCache(String key){
		
		try {
			String[] urls = key.split(",");
			Map<String,Object> _map = new HashMap<String,Object>(); ;
			for(String url : urls) {
				if(!StringUtils.isBlank(url)) {
					if(url.indexOf(".") > 0) {
						String[] cache_keys = url.split("\\.");
						_map.put(cache_keys[0], cache_keys[1]);
					}else {
						_map.put(url, url);
					}
					
				}
				
			}
			
			if(_map != null && _map.size() > 0) {
				LocalCache.localCache.put(LocalCache.FILTER_URL_ONE, _map);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		}catch (Exception e) {
			logger.error("更新本地缓存失败: ",e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
		
	}
	
	
	
	//更新拦截URL
	@RequestMapping(value="/update_local_filter_url_db")
	public @ResponseBody ResponseEntity<?> updateLocalCacheForDB(String keys){
		
		try {
			//直连库查询
			BaseRestrictions baseRestrictions = baseRestrictionsService.selectAllBaseRestrictionsByType(keys);
			if(baseRestrictions != null) {
				String key = baseRestrictions.getName();
				String[] urls = key.split(",");
				Map<String,Object> _map = new HashMap<String,Object>(); ;
				for(String url : urls) {
					
					if(!StringUtils.isBlank(url)) {
						if(url.indexOf(".") > 0) {
							String[] cache_keys = url.split("\\.");
							_map.put(cache_keys[0], cache_keys[1]);
						}else {
							_map.put(url, url);
						}
						
					}
					
				}
				
				if(_map != null && _map.size() > 0) {
					LocalCache.localCache.put(LocalCache.FILTER_URL_ONE, _map);
				}
				
			}
			
			
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		}catch (Exception e) {
			logger.error("更新本地缓存失败: ",e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
		
	}
	
	
	

}
